﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using SharedLibrary;

namespace CLinq
{
    public class CustomerQueries
    {
        public static XElement XMLLiteral()
        {
            XElement x = new XElement("Customers",
                               new XElement("Customer",
                                            new XAttribute("Name","Bob Jones"),
                                            new XElement("Order", new XAttribute("Product", "Milk"), new XAttribute("Quantity", 2)),
                                            new XElement("Order", new XAttribute("Product", "Cereal"), new XAttribute("Quantity", 10)),
                                            new XElement("Order", new XAttribute("Product", "Apples"), new XAttribute("Quantity", 5))
                                            )
                               );
            return x;
        }

        public static XElement CreateCustomerXML(SharedLibrary.Customer[] customers)
        {
            return new XElement("Customers",
                                from c in customers 
                                select new XElement("Customer",
                                                    new XAttribute("Name",c.FirstName + " " + c.LastName),
                                                    from o in c.Orders 
                                                    select new XElement("Order", 
                                                                        new XAttribute("Product", o.Product), 
                                                                        new XAttribute("Quantity", o.Quantity))
                                            )
                               );
        }

        public static Customer[] ReadCustomerXMLIntoCustomer(XElement customerXml)
        {
            var results = from cust in customerXml.Elements("Customer")
                          let  nameBits = cust.Attribute("Name").Value.Split(' ') 
                          select new Customer() { FirstName = nameBits[0],LastName=nameBits[1] };
            return results.ToArray();

        //Dim results = From cust In customerXml.<Customer> _
        //              Let nameBits = cust.@Name.Split(" "c) _
        //              Select New Customer() With {.FirstName = nameBits(0), .LastName = nameBits(1)}

        //ObjectDumper.Write(results, log:=Me.mOutStream)
        }

    }

    
}
